package com.ruoyi.web.controller.basice_info;

import com.ruoyi.basice_info.domain.TbAcademicYear;
import com.ruoyi.basice_info.service.ITbAcademicYearService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 学年设置Controller
 *
 * @author hongyi
 * @date 2023-12-07
 */
@RestController
@RequestMapping("/basice_info/year")
public class TbAcademicYearController extends BaseController
{
    @Autowired
    private ITbAcademicYearService tbAcademicYearService;

    /**
     * 查询学年设置列表
     */
    @PreAuthorize("@ss.hasPermi('basice_info:year:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbAcademicYear tbAcademicYear)
    {
        startPage();
        List<TbAcademicYear> list = tbAcademicYearService.selectTbAcademicYearList(tbAcademicYear);
        return getDataTable(list);
    }

    /**
     * 导出学年设置列表
     */
    @PreAuthorize("@ss.hasPermi('basice_info:year:export')")
    @Log(title = "学年设置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbAcademicYear tbAcademicYear)
    {
        List<TbAcademicYear> list = tbAcademicYearService.selectTbAcademicYearList(tbAcademicYear);
        ExcelUtil<TbAcademicYear> util = new ExcelUtil<TbAcademicYear>(TbAcademicYear.class);
        util.exportExcel(response, list, "学年设置数据");
    }

    /**
     * 获取学年设置详细信息
     */
    @PreAuthorize("@ss.hasPermi('basice_info:year:query')")
    @GetMapping(value = "/{academicYearId}")
    public AjaxResult getInfo(@PathVariable("academicYearId") Long academicYearId)
    {
        return success(tbAcademicYearService.selectTbAcademicYearByAcademicYearId(academicYearId));
    }

    /**
     * 新增学年设置
     */
    @PreAuthorize("@ss.hasPermi('basice_info:year:add')")
    @Log(title = "学年设置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbAcademicYear tbAcademicYear)
    {
        addOrEdit(tbAcademicYear,"add");
        return toAjax(tbAcademicYearService.insertTbAcademicYear(tbAcademicYear));
    }

    /**
     * 修改学年设置
     */
    @PreAuthorize("@ss.hasPermi('basice_info:year:edit')")
    @Log(title = "学年设置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbAcademicYear tbAcademicYear)
    {
        addOrEdit(tbAcademicYear,"edit");
        return toAjax(tbAcademicYearService.updateTbAcademicYear(tbAcademicYear));
    }

    public void addOrEdit(TbAcademicYear tbAcademicYear,String type)
    {
        TbAcademicYear yearCode = tbAcademicYearService.getTbAcademicYearByCode(tbAcademicYear.getAcademicYearCode());
        TbAcademicYear yearName = tbAcademicYearService.getTbAcademicYearByName(tbAcademicYear.getAcademicYearName());
        if(yearCode!=null){
            if(type.equals("add")){
                throw new IllegalArgumentException("学年代码已存在");
            }else{
                if(!tbAcademicYear.getAcademicYearId().equals(yearCode.getAcademicYearId())){
                    throw new IllegalArgumentException("学年代码已存在");
                }
            }
        }
        if(yearName!=null){
            if(type.equals("add")){
                throw new IllegalArgumentException("学年名称已存在");
            }else{
                if(!tbAcademicYear.getAcademicYearId().equals(yearName.getAcademicYearId())){
                    throw new IllegalArgumentException("学年名称已存在");
                }
            }
        }
    }

    /**
     * 删除学年设置
     */
    @PreAuthorize("@ss.hasPermi('basice_info:year:remove')")
    @Log(title = "学年设置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{academicYearIds}")
    public AjaxResult remove(@PathVariable Long[] academicYearIds)
    {
        return toAjax(tbAcademicYearService.deleteTbAcademicYearByAcademicYearIds(academicYearIds));
    }
}
